package com.cswnode.demo.controller;

import com.cswnode.demo.entity.Page;
import com.cswnode.demo.service.EnterpriseUserService;
import com.cswnode.demo.util.ResponseVo;
import com.cswnode.demo.vo.EnterpriseUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author csw
 * @create 2021/3/18 14:38
 */
@Api(tags = "企业用户管理")
@RequestMapping("/enterpriseUser")
@RestController
public class EnterpriseUserController {

    @Autowired
    private EnterpriseUserService enterpriseUserService;

    @ApiOperation(value = "添加企业用户类型")
    @PostMapping("/add")
    public ResponseVo add(@RequestBody EnterpriseUserVo enterpriseUserVo){
        if (enterpriseUserVo == null){
            return ResponseVo.error("企业用户信息不能为空！");
        }
        String insert = enterpriseUserService.insertUser(enterpriseUserVo);
        if (insert == null){
            return ResponseVo.success("添加成功！");
        }
        return ResponseVo.error(insert);
    }

    @ApiOperation(value = "分页查询企业用户类型")
    @GetMapping("/query")
    public ResponseVo query(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10")Integer limit, String userNameCn){
        Map<String,Object> result = new HashMap<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setLimit(limit);
        page.setRows(enterpriseUserService.selectNum(userNameCn));
        List<EnterpriseUserVo> enterpriseUserVos = enterpriseUserService.queryAllByVo(page.getOffset(), page.getLimit(), userNameCn);
        result.put("page",page);
        result.put("enterpriseUserVos",enterpriseUserVos);
        return ResponseVo.success(result);
    }


    @ApiOperation(value = "修改企业用户")
    @PostMapping("/update")
    public ResponseVo update(@RequestBody EnterpriseUserVo enterpriseUserVo){
        if (ObjectUtils.isEmpty(enterpriseUserVo)){
            return  ResponseVo.error("企业信息不得为空！");
        }
        String update = enterpriseUserService.update(enterpriseUserVo);
        return ResponseVo.success(update);
    }

    @ApiOperation(value = "删除企业用户")
    @GetMapping("/delete/{userId}")
    public ResponseVo delete (@PathVariable("userId")Long userId){
        if (userId == null){
            return ResponseVo.error("删除失败！");
        }
        boolean result = enterpriseUserService.deleteById(userId);
        if (!result){
            return ResponseVo.error("删除失败");
        }
        return ResponseVo.success("删除成功");
    }

    @ApiOperation(value = "获得当前用户所在的企业id")
    @GetMapping("/getEntId")
    public ResponseVo getEntId(){
        Long entId =  enterpriseUserService.getEntId();
        if (entId == null){
            entId = 0L;
        }
        return ResponseVo.success(entId);
    }


}
